package com.teamwhole.modules.sys.web;

import java.util.Map;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.github.pagehelper.Page;
import com.teamwhole.common.model.ResultInfo;
import com.teamwhole.modules.sys.entity.User;
import com.teamwhole.modules.sys.service.UserService;

import jersey.repackaged.com.google.common.collect.Maps;

@Component
@Path("/sys/user")
@Consumes("application/json;charset=utf-8")
@Produces("application/json;charset=utf-8")
public class UserResource {
	
	@Autowired
	private UserService userService;
	
	@Path("/get")
	@GET
	public User get(@QueryParam("id") String id) {
		return userService.get(id);
	}
	
	@Path("/findByLoginName")
	@GET
	public User findByLoginName(@QueryParam("loginName") String loginName) {
		return userService.findByLoginName(loginName);
	}
	
	@Path("/list")
	@POST
	public Object list(User model) {
		Page<User> page = userService.findPage(model);
		Map<String, Object> map = Maps.newHashMap();
		map.put("total", page.getTotal());
		map.put("rows", page.getResult());
		return map;
	}
	
	@Path("/create")
	@POST
	public ResultInfo create(User model) {
		userService.save(model);
		return ResultInfo.ok();
	}
	
	@Path("/update")
	@POST
	public ResultInfo update(User model) {
		userService.save(model);
		return ResultInfo.ok();
	}
	
	@Path("/delete")
	@GET
	public ResultInfo delete(@QueryParam("id") String id) {
		userService.delete(id);
		return ResultInfo.ok();
	}
	
	@Path("/modify_password")
	@POST
	public ResultInfo modifyPassword(User model){
		userService.modifyPassword(model);
		return ResultInfo.ok();
	}
	
	@Path("/init_password")
	@GET
	public ResultInfo initPassword(@QueryParam("id") String id){
		userService.initPassword(new User(id));
		return ResultInfo.ok();
	}
	
}
